package com.customview.customPic;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Shader;
import android.media.Image;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import com.customview.R;

/**
 * @author Sandsboy
 * @description:广州皓醒科技有限公司
 * @date :2019/10/11 16:29
 */
public class CustomPic extends android.support.v7.widget.AppCompatImageView {
    public CustomPic(Context context) {
        this(context, null);
    }

    public CustomPic(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CustomPic(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        init();
    }

    Paint mPaint;

    private void init() {

        mPaint = new Paint();


    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        drawPic(canvas);
    }


    private void drawPic(Canvas canvas) {

        Bitmap bitmap2 = BitmapFactory.decodeResource(getResources(), R.mipmap.icon);
        int width = bitmap2.getWidth();
        int height = bitmap2.getHeight();


        float scal=800/height;
        float scal2=800/width;
        Matrix matrix = new Matrix();
        matrix.postScale(scal2, scal);
        Bitmap bitmap = Bitmap.createBitmap(bitmap2, 0, 0, width, height, matrix, true);
//        Log.d("处理后的宽高",bitmap.getWidth()+"::"+bitmap.getHeight());
//        Bitmap bitmap = Bitmap.createScaledBitmap( bitmap2,800, 800, true);

        BitmapShader shader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
        mPaint.setShader(shader);

        canvas.drawCircle(400,400,400,mPaint);


    }

}
